//index.js
var md5 = require('../../utils/md5.js');
//获取应用实例
var app = getApp()
Page({
  data: {
    motto: 'Hello World',
    userInfo: {},
    loading:false
  },
  //事件处理函数
  bindViewTap: function() {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  onLoad: function () {
    var that = this
    //调用应用实例的方法获取全局数据
    app.getUserInfo(function(userInfo){
      //更新数据
      that.setData({
        userInfo:userInfo
      })
    })
  },
  setLoading: function(e) {
    var that = this;
    that.setData({
      loading: true
    })
  },
  formSubmit: function(e){
    var that = this;
    if(e.detail.value.loginName.length < 1 || e.detail.value.pwd.length < 1){
      wx.showModal({
        // title: '提示',
        showCancel:false,
        content: '账号或密码不能为空！',
        success: function(res) {
          that.setData({
            loading: false
          });
          if (res.confirm) {
            console.log('用户点击确定');
          } else if (res.cancel) {
            console.log('用户点击取消')
          }
        }
      });
    return;
    }
    wx.request({
      url: app.globalData.API + '/applogin',
      method:'POST',
      header: {
        "Cookie":wx.getStorageSync('sessionid')
      },
      data: {
        "locale": "zh",
        "username": e.detail.value.loginName,
        "password": e.detail.value.pwd,
        "channid": "1"
      },
      success: function(res) {
        console.log(res);
        if(res.data.code == 10000){
          wx.showToast({
            title: '登录成功！',
            icon: 'success',
            duration: 2000
          })
          wx.setStorageSync('loginInfo', res.data);
          setTimeout(function(){
            wx.switchTab({
            url: '../work/list'
          })
          },2500);
        }else{
          wx.showModal({
            title: res.data.msg,
            showCancel:false,
            // content: '账号或密码不能为空！',
            // success: function(res) {
            //   if (res.confirm) {
            //     console.log('用户点击确定');
            //   } else if (res.cancel) {
            //     console.log('用户点击取消')
            //   }
            // }
          });
        }
      },
      fail: function(){
        console.log('接口调用失败');
      },
      complete: function(){
        that.setData({
            loading: false
          });
      }
    })
    console.log('form发生了submit事件，携带数据为：',e.detail.value);
  }
})
